import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/home/Index'
import Main from '@/components/home/Main'
import Main1 from '@/components/home/Main1'
import Login from '@/components/login/Login'
import Register from '@/components/login/Register'

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err);
}

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/index',
      name: 'Index',
      component: Index,
      meta: {                 
        root: true   // 要求验证的页面,在此情况下其子页面也会被验证.
           },
      children: [
        　　　{
        　　　　path: '/search',
         　　　 component: Main
        　　  },
              {
          　　　　path: '/chose',
           　　　 component: Main1
          　　 }
        　　]
    },
    {
      path: '/',
      name: 'Login',
      component: Login,
    },
    {
      path: '/register',
      name: 'Register',
      component: Register,
    }
  ],

})
router.beforeEach((to,from,next)=>{
  if(to.meta.root) {
    if(window.sessionStorage.getItem('token')) {
      next();
    }else{
      next({ path:'/'})
    }
  }else{
    next();
  }
})

export default router
